package com.basic.service.dto;

import cn.hutool.core.util.StrUtil;
import com.basic.pojo.MyTableFieldInfo;
import lombok.*;

import java.util.List;

/**
 * 用户定义标准的类
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GenParam {

    @Getter
    @AllArgsConstructor
    public static enum Type {
        CONTROLLER(1, "vm/java/controller.java.vm", "{}/controller/{}Controller.java", "mvc", false, "controller模板路径"),
        SERVICE(2, "vm/java/service.java.vm", "{}/service/{}Service.java", "mvc",false, "service模板路径"),
        SERVICE_IMPL(3, "vm/java/serviceImpl.java.vm","{}/service/impl/{}ServiceImpl.java", "mvc",false, "serviceImpl模板路径"),
        POJO(4, "vm/java/pojo.java.vm", "{}/pojo/{}.java","mvc", false, "pojo模板路径"),
        DTO(5,"vm/java/dto.java.vm", "{}/service/dto/{}Dto.java", "mvc",false, "dto模板路径"),
        QUERY_CRITERIA(6, "vm/java/queryCriteria.java.vm", "{}/service/vo/{}QueryCriteria.java", "mvc", false, "queryCriteria模板路径"),
        MAPPER(7, "vm/java/mapper.java.vm", "{}/mapper/{}Mapper.java", "mvc",false, "mapper模板路径"),

        API_RESULT(8,"vm/java/ApiResult.java.vm", "{}/common/ApiResult.java", "extra", true, "ApiResult模板路径"),
        BASE_POJO(9, "vm/java/BasePojo.java.vm", "{}/pojo/BasePojo.java", "extra", true, "basePojo模板路径"),
        MY_CONVERTER_UTILS(10, "vm/java/MyConverterUtils.java.vm", "{}/utils/MyConverterUtils.java", "extra", true, "myConverterUtils模板路径"),
        PAGE_RESULT(11, "vm/java/PageResult.java.vm", "{}/common/PageResult.java", "extra", true, "pageResult模板路径"),
        QUERY(12, "vm/java/Query.java.vm", "{}/annotation/Query.java", "extra", true, "query模板路径"),
        RESPONSE_CODE(13, "vm/java/ResponseCode.java.vm", "{}/common/ResponseCode.java", "extra", true, "responseCode模板路径"),
        QUERY_HELP_PLUS(14,"vm/java/QueryHelpPlus.java.vm", "{}/utils/QueryHelpPlus.java", "extra", true, "queryHelpPlus模板路径"),

        MAPPER_XML(15,"vm/xml/mapper.xml.vm", "{}/mapper/xml/{}Mapper.xml", "xml", false, "mapperXml模板路径"),

        API_JS(16,"vm/js/api.js.vm", "{}/controller/js/{}Api.js", "js", false, "ApiJs模板路径"),

        CONFIG_YML(17,"vm/yml/application.yml.vm", "{}/config/application.yml", "yml", true, "application.yml模板路径"),
        CONFIG_YML_DEV(18,"vm/yml/application-dev.yml.vm", "{}/config/application-dev.yml", "yml", true, "application-dev模板路径"),
        CONFIG_YML_PROD(19,"vm/yml/application-dev.yml.vm", "{}/config/application-prod.yml", "yml", true, "application-prod模板路径"),

        ;
        int val;
        String path;
        String filePathTemplate;
        String type;
        boolean isOne;
        String desc;

        public static Type valueOf(int val) {
            for (Type each : values()) {
                if (each.val == val) {
                    return each;
                }
            }

            return null;
        }


    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TableInfo {
        /**
         * 数据库表名
         */
        private String tableName;

        /**
         * 生实体类名
         */
        private String className;

        /**
         * 生成实体类变量名，比如UserInfo 变成 userInfo
         */
        private String classNameSmall;

        /**
         * 数据库表表述内容
         */
        private String tableComment;

        /**
         * 数据库中的表列信息
         */
        private List<MyTableFieldInfo> fieldInfos;
    }

    private Integer port;

    private String driverClass;

    private String url;

    private String username;

    private String passwd;

    private String author;

    private String module;

    private String packagePath;

    private TableInfo tableInfo;

    /**
     * yyyy-MM-dd
     */
    private String createTime;

    private Integer type;

    /**
     * controller路径
     */
    private String controllerPath;

    private List<TableInfo> tableInfoList;

    public String getPackagePathFormat() {
        return this.packagePath.replaceAll("\\.", "/");
    }

    public String getZipFileName() {
        return this.module + ".zip";
    }

    public static GenParam.TableInfo getTableInfo(String tableName, String tableComment) {
        final String tableNameSmall = StrUtil.toCamelCase(tableName);
        final StringBuilder className = new StringBuilder();
        for (int i = 0; i < tableNameSmall.length(); i++) {
            if (i == 0) {
                className.append(String.valueOf(tableNameSmall.charAt(i)).toUpperCase());
            } else {
                className.append(tableNameSmall.charAt(i));
            }
        }
        return TableInfo.builder().tableName(tableName).tableComment(tableComment).className(className.toString()).classNameSmall(tableNameSmall).build();
    }


}
